/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.partaljevic.tapestry.gambling.pages;

import com.partaljevic.gambling.beans.Match;
import com.partaljevic.gambling.beans.Result;
import com.partaljevic.tapestry.gambling.service.MatchesService;
import com.partaljevic.tapestry.gambling.service.ResultService;
import com.partaljevic.tapestry.gambling.service.TeamService;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.upload.services.UploadedFile;

/**
 *
 * @author Nemanja
 */
public class AdminMatchResult {

    @Persist(PersistenceConstants.FLASH)
    private String message;
    @InjectPage
    private Working work;
    @Property
    private UploadedFile fileM;
    @Property
    private UploadedFile fileR;
    @Inject
    @Property
    private MatchesService matchService;
    @Inject
    private ResultService resultService;
    @Inject
    private TeamService teamService;
    @Property
    private Match match;
    @Property
    private String matchResult;
    @Property
    private String home;
    @Property
    private String away;
    @Property
    private String matchStart;
    @Property
    private double kec;
    @Property
    private double iks;
    @Property
    private double dvojka;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void onSubmitFromMatches() {
        try {
            work.readList(fileM.getStream());
            message = "Successfully uploaded list of matches";
        } catch (Exception ex) {
            message = "Error: uploaded file must be xml file";
        }
    }

    public void onSubmitFromResults() {
        try {
            work.readResult(fileR.getStream());
            message = "Successfully uploaded results";
        } catch (Exception ex) {
            message = "Error: uploaded file must be xml file";
        }
    }

    void onSubmitFromAddMatch() {
        Match match1 = new Match();
        match1.setAway(away);
        match1.setDvojka(dvojka);
        match1.setHome(home);
        match1.setIks(iks);
        match1.setKec(kec);
        match1.setMatchStart(matchStart);
        matchService.save(match1);
        message = "Match successfully saved!";
    }

    void onSubmitFromAddResult() {
        Result result = new Result(match, matchResult);
        resultService.save(result);
        work.checkPairs(result);
        work.checkTeams(result);
        work.checkTickets();
        message = "Result successfully saved!";
    }

    Object onUploadException(FileUploadException ex) {
        message = "Upload exception: " + ex.getMessage();
        return this;
    }
}
